package com.groupbuying.customer.controller;

import com.groupbuying.customer.constants.ModileCodeTypeConstants;
import com.groupbuying.customer.utils.JSONUtils;
import com.groupbuying.customer.utils.ParamUtils;
import com.groupbuying.customer.utils.ReturnUtils;
import com.groupbuying.customer.service.ModileCodeService;
import com.groupbuying.customer.service.PictureCodeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

/**
 * @anthor wenguoqiang
 * @date: 2019/01/15 16:23
 * @company 蒙泰互联网公司
 * @des
 **/

@Api("用户获取验证码接口")
@RestController
@RequestMapping("/tool")
public class ModileCodeController {

    private static Logger log = LoggerFactory.getLogger(ModileCodeController.class);

    @Autowired
    private PictureCodeService pictureCodeService;

    @Autowired
    private ModileCodeService modileCodeService;

    /**
     * @api {post} /tool/getRegisterCode 获用于用户注册时获取注册用的手机验证码
     * @apiExample
     * author mayunfei
     * tar 用于:TODO 未知调用方
     * date 2019/10/31
     * @apiGroup bus-customer
     * @apiVersion 1.0.0
     * @apiHeader {String} Authorization 用户授权token
     * @apiParam {HttpServletResponse} response:相应
     * @apiParam {HttpServletRequest} request:请求
     * @apiSuccess {Object} ReturnUtils.appendResultStr:通用日志记录
     */
    @ApiOperation(value = "获取注册验证码", notes = "用于用户注册时获取注册用的手机验证码")
    @RequestMapping(value = {"/getRegisterCode"}, produces = {"text/html;charset=UTF-8"}, method = {
            RequestMethod.POST})
    public Object getRegisterCode(HttpServletResponse response, HttpServletRequest request) {
        String responseStr = null;
        //Long startTime = System.currentTimeMillis();
        Map<String, Object> mapParams = ParamUtils.getParamMap(request);
        mapParams.put("type", ModileCodeTypeConstants.APP_REGISTER.getCode());
        Map map = null;
        try {
            map = modileCodeService.getModileCodeServiceCode(mapParams);
        } catch (Exception e) {
            e.printStackTrace();
            map = ReturnUtils.getExceptionResult(log, e);
        } finally {
            //System.out.println("获取注册验证码用时====" + (System.currentTimeMillis() - startTime));
            responseStr = JSONUtils.beanToJson(map);
        }
        return ReturnUtils.appendResultStr(log, responseStr, "getRegisterCode");
    }

    /**
     * @api {post} /tool/getCustomerResetPwdCode  获取会员变更密码验证码
     * @apiExample
     * author mayunfei
     * tar 用于:TODO 未知调用方
     * date 2019/10/31
     * @apiGroup bus-customer
     * @apiVersion 1.0.0
     * @apiHeader {String} Authorization 用户授权token
     * @apiParam {HttpServletResponse} response:相应
     * @apiParam {HttpServletRequest} request:请求
     * @apiSuccess {Object} ReturnUtils.appendResultStr:通用日志记录
     */
    @ApiOperation(value = "获取会员变更密码验证码", notes = "用于会员变更密码时获取手机验证码")
    @RequestMapping(value = {"/getCustomerResetPwdCode"}, produces = {"text/html;charset=UTF-8"}, method = {RequestMethod.POST})
    public Object getCustomerChangePwdCode(HttpServletResponse response, HttpServletRequest request) {
        String responseStr = null;
        //Long startTime = System.currentTimeMillis();
        Map<String, Object> mapParams = ParamUtils.getParamMap(request);
        mapParams.put("type", ModileCodeTypeConstants.FIND_PWD.getCode());
        Map map = null;
        try {
            map = modileCodeService.getModileCodeServiceCode(mapParams);
        } catch (Exception e) {
            e.printStackTrace();
            map = ReturnUtils.getExceptionResult(log, e);
            //System.out.println("获取变更验证码用时====" + (System.currentTimeMillis() - startTime));
        } finally {
            responseStr = JSONUtils.beanToJson(map);
        }
        return ReturnUtils.appendResultStr(log, responseStr, "getCustomerChangePwdCode");

    }
    /**
     * @api {post} /tool/checkCustomerCode 重置登录密码校验
     * @apiExample
     * author mayunfei
     * tar 用于:TODO 未知调用方
     * date 2019/10/31
     * @apiGroup bus-customer
     * @apiVersion 1.0.0
     * @apiHeader {String} Authorization 用户授权token
     * @apiParam {Map} mobile:手机号
     * @apiParam {Map} captcha:验证码
     * @apiParam {Map} type:类型
     * @apiSuccess {ApiResult} success:登录成功; failure:请求
     * @apiSuccess {ApiResult}  failure:请求
     * @apiSuccess {ApiResult}  access_token失败
     */
    @ApiOperation(value = "重置登录密码校验", notes = "确保用户使用本人手机")
    @PostMapping("/checkCustomerCode")
    public Map checkCustomerCode(@RequestParam Map<String, Object> mapParams) {
        mapParams.put("type", ModileCodeTypeConstants.FIND_PWD.getCode());
        return modileCodeService.checkModileCode(mapParams);
    }

}
